import { baseURLWebApi } from "./env.js";

// 需要修改token，和根据实际修改请求头
export default (url, method, params, isJson) => {
  // 获取本地token
  const token = uni.getStorageSync("token");
  // const systemAppId = uni.getStorageSync('systemAppId');
  //判断进入页面的时候或者本地有没有token,有就直接值某一页，没有则请求接口
  // if (token || url == "api/User/Login") {
  // if (url != 'api/EnergyCalculation/GetReportImg' && url != 'api/EnergyCalculation/GetThreeModelImg' && url !=
  // 	'api/EnergyCalculation/DepthReportCalculation') {
  uni.showLoading({
    title: "加载中",
  });
  // }
  // 根据请求方法或URL来判断是否添加请求头
  let header = {};
  if (token) {
    header["Authorization"] = `Bearer ${token}`;
  }
  header["content-type"] = isJson
    ? "application/json; charset=utf-8"
    : "application/x-www-form-urlencoded";
  return new Promise((resolve, reject) => {
    uni.request({
      url: baseURLWebApi + url,
      method: method,
      header: header,
      data: params,
      success(response) {
        // console.log('response', response)
        uni.hideLoading();
        const res = response;
        // 根据返回的状态码做出对应的操作
        if (res.statusCode === 200) {
          if (res.extra.error_code === 0) {
            resolve(res.data);
          } else {
            reject(res.message);
          }
        } else {
          uni.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 6000,
          });
          reject();
        }
      },
      fail(err) {
        uni.hideLoading();
        reject(err);
      },
    });
  }).catch((e) => {});
};
